package top.foxhome.productimages.productimages;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.stage.DirectoryChooser;
import javafx.stage.Window;

import java.io.File;
import java.util.*;

public class MainController {
    private String defaultFilePath;
    public static final String MAIN_DIRECTORY_NAME = "主图";
    public static final String DETAIL_DIRECTORY_NAME = "商品详情";
    public static final String BASE_DIRECTORY_NAME = "商品";
    private ConfigProperties configProperties;
    @FXML
    private Label welcomeText;
    private File rootFile;

    public void initView() {
        configProperties = ConfigProperties.getInstance();
        defaultFilePath = configProperties.getValue(ConfigProperties.CONFIG_KEY_WORK_DIRECTORY, FileUtils.getDesktopPath() + "\\" + BASE_DIRECTORY_NAME);
        welcomeText.setText(String.format(I18N.get("label.workingdirectory"), defaultFilePath));
        rootFile = new File(defaultFilePath);
        if (!rootFile.exists()) rootFile.mkdirs();
    }

    private Window ownerWindow;

    public void setOwnerWindow(Window ownerWindow) {
        this.ownerWindow = ownerWindow;
    }

    /**
     * 清理图片文件
     */
    @FXML
    protected void onCleanButtonClick() {
        if (!rootFile.exists()) return;
        //清理图片文件点击
        List<File> delFiles = new ArrayList<File>();
        File[] rootChildFiles = rootFile.listFiles(File::isFile);
        if (rootChildFiles != null) {
            delFiles.addAll(Arrays.asList(rootChildFiles));
        }
        File[] mainChildFiles = new File(rootFile, MAIN_DIRECTORY_NAME).listFiles(File::isFile);
        if (mainChildFiles != null) {
            delFiles.addAll(Arrays.asList(mainChildFiles));
        }
        File[] detailChildFiles = new File(rootFile, DETAIL_DIRECTORY_NAME).listFiles(File::isFile);
        if (detailChildFiles != null) {
            delFiles.addAll(Arrays.asList(detailChildFiles));
        }
        delFiles.forEach(File::delete);
    }


    /**
     * 重命名图片文件
     */
    @FXML
    public void onRemaneButtonClick(ActionEvent actionEvent) {
        //过滤出图片文件
        File[] imageFiles = rootFile.listFiles(pathname -> pathname.isFile() && pathname.getName().matches("(?i)[^\\\\/?*:|\"<>\\\\.]+\\.(jpg|jpeg|png)$"));
        //如果文件目录不含图片文件则不进行处理
        if (imageFiles == null || imageFiles.length == 0) return;
        Map<String, String> newFileName = new HashMap<String, String>();
        //文件重命名格式修改为width_height_时间戳_随机数
        for (int i = 0; i < imageFiles.length; i++) {
            File imageFile = imageFiles[i];
            ImageSize imageSize = FileUtils.getImageWH(imageFile);
            File newNameFile = null;
            do {
                String newName = TextUtil.getNewFileName(imageSize.getWidth(), imageSize.getHeight(), imageFile.getName());
                newNameFile = new File(imageFile.getParentFile(), newName);
            } while (newNameFile.exists());
            imageFile.renameTo(newNameFile);
        }
    }

    @FXML
    public void onChangeButtonClick(ActionEvent actionEvent) {
        //重新选择文件目录
        // 创建 DirectoryChooser 实例
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("请选择文件夹");
        directoryChooser.setInitialDirectory(new File(System.getProperty("user.home")));
        // 显示文件夹选择对话框
        File selectedDirectory = directoryChooser.showDialog(ownerWindow);
        if (selectedDirectory != null) {
            String newPath = selectedDirectory.getAbsolutePath() + "\\" + BASE_DIRECTORY_NAME;
            configProperties.setValue(ConfigProperties.CONFIG_KEY_WORK_DIRECTORY, newPath);
            configProperties.save();
            if (rootFile.exists()) FileUtils.deleteFile(rootFile);
            rootFile = new File(newPath);
            if (!rootFile.exists()) rootFile.mkdirs();
            File mainFile = new File(rootFile, MAIN_DIRECTORY_NAME);
            if (!mainFile.exists()) mainFile.mkdirs();
            File detailFile = new File(rootFile, DETAIL_DIRECTORY_NAME);
            if (!detailFile.exists()) detailFile.mkdirs();
            defaultFilePath = newPath;
            welcomeText.setText(String.format(I18N.get("label.workingdirectory"), defaultFilePath));
        }
    }


}